/**
 * Created by Intellij IDEA
 *
 * @author 王俊凯
 * @Date: 2023/3/2 15:25
 * @Version 1.0
 */
package com.eight.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eight.pojo.TbCar;
import com.eight.pojo.TbOrder;
import com.eight.service.CarInformationService;
import com.eight.service.OrderService;
import com.eight.vo.CarOrderVo;
import com.eight.vo.ComResult;
import com.eight.vo.OrderPayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/rent/order")
@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private CarInformationService carInformationService;

    @PostMapping("queryAllOrder/{page}/{size}")
    public ComResult queryAllOrder(@PathVariable(value = "page") Integer page, @PathVariable(value = "size") Integer size, @RequestBody TbOrder tbOrder) {

        return new ComResult(2000, "查询到所有的订单", orderService.queryAllOrder(page, size, tbOrder));
    }

    @PostMapping("deleteOrederById/{id}")
    public ComResult deleteOrederById(@PathVariable(value = "id") Integer id) {
        return new ComResult(2000, "删除订单成功", orderService.removeById(id));
    }

    @PostMapping("updateOreder/{id}")
    public ComResult deleteO(@RequestBody TbOrder tbOrder) {
        return new ComResult(2000, "", orderService.updateById(tbOrder));
    }

    @PostMapping("insertOreder")
    public ComResult insertO(@RequestBody CarOrderVo carOrderVo) {
        return orderService.makeOrder(carOrderVo);
    }

    /**
     * 根据车辆订单信息生产二维码并且插入一条订单信息，并且推送给RabbitMq
     *
     * @param carOrderVo 订单信息
     * @return 插入的结果
     */
    @PostMapping("productionAliPay")
    public ComResult productionAliPay(@RequestBody CarOrderVo carOrderVo) {
        return orderService.productionAliPay(carOrderVo);
    }

    /**
     * 生产微信二维码
     *
     * @param carOrderVo
     * @return
     */
    @PostMapping("productionWxPay")
    public ComResult productionWxPay(@RequestBody CarOrderVo carOrderVo) {
        return orderService.productionWxPay(carOrderVo);
    }

    /**
     * 根据uuid来查询订单状态
     *
     * @param uuid uuid
     * @return 查询的订单结果
     */
    @GetMapping("getOrderByUuid/{uuid}")
    public TbOrder getOrderByUuid(@PathVariable String uuid) {
        return orderService.getOne(new QueryWrapper<TbOrder>().eq("orderNo", uuid));
    }

    /**
     * 修改订单的状态根据uuid
     *
     * @param state 订单的状态 0未支付，1已支，4超时
     * @param uuid  订单的uuid
     * @return 订单的结果
     */
    @GetMapping("changeStateByUuid/{uuid}/{state}")
    public ComResult changeStateByUuid(@PathVariable Integer state, @PathVariable String uuid) {
        // 更新订单信息
        boolean orderNo = orderService.update(new TbOrder().setState(state), new UpdateWrapper<TbOrder>().eq("orderNo", uuid));
        // 获取订单的具体信息
        TbOrder orderNo1 = orderService.getOne(new QueryWrapper<TbOrder>().eq("orderNo", uuid));
        // 修改车辆状态
        carInformationService.updateCar(new TbCar().setState(1).setId(orderNo1.getCarId()));
        return orderNo ? new ComResult(2000, "修改成功") : new ComResult(5000, "修改失败");
    }


    /**
     * 校验订单状态
     *
     * @param uuid 支付宝的订单id
     * @return 支付成功状态
     */
    @GetMapping("checkOrderStatu/{uuid}")
    public ComResult checkOrderStatu(@PathVariable String uuid) {
        OrderPayVo orderPayVo = new OrderPayVo();
        orderPayVo.setOrderNo(uuid);
        return orderService.checkOrderStatu(orderPayVo);
    }

    /**
     * 检验微信订单状态
     *
     * @param uuid
     * @return
     */
    @GetMapping("checkOrderStatus/{uuid}")
    public ComResult checkOrderStatus(@PathVariable String uuid) {
        OrderPayVo orderPayVo = new OrderPayVo();
        orderPayVo.setOrderNo(uuid);
        return orderService.checkOrderStatus(orderPayVo);
    }

}

